package com.aaa.bbb.mymaintain.dao;

import com.aaa.bbb.mymaintain.entity.IssuesEmployee;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author max
 * @since 2023-11-03
 */
public interface IssuesEmployeeDao extends BaseMapper<IssuesEmployee> {

    @Select({
            "<script> " +
                    "SELECT "   +
                    "    issues_employee.issue_id, "   +
                    "    issues_employee.job_id, "   +
                    "    employee.name, "   +
                    "    issues_employee.detail, "   +
                    "    picture, "   +
                    "    issues_employee.state, "   +
                    "    explanation, "   +
                    "    issues_employee.date "   +
                    "FROM   issues_employee "   +
                    "   INNER JOIN employee ON issues_employee.job_id = employee.job_id "   +
                    "WHERE  1=1 " +
                    "  <if test='userName != null and userName != \"\" '>" +
                    "     AND username = #{userName} " +
                    "  </if>"   +
                    "ORDER BY issues_employee.issue_id DESC " +
                    "</script>"
    })
    public List<Map<String, Object>> selectByCond(@Param("userName") String userName);


    @Select({
            "<script> " +
                    "SELECT "   +
                    "    unapproval_reason, "   +
                    "    unfund_reason, "   +
                    "    applyfund_reason, "   +
                    "    maintenance_notqualified_reason, "   +
                    "    cannot_repair_reason,  "   +
                    "    persist_reason "   +
                    "FROM   issues_employee "   +
                    "WHERE  1=1 " +
                    "  <if test='issue_id != null and issue_id != \"\" '>" +
                    "     AND issue_id = #{issue_id} " +
                    "  </if>"   +
                    "</script>"
    })
    public Map<String, String> selectByIssueId(@Param("issue_id") String issue_id);




    @Select({
            "<script> " +
                    "SELECT "   +
                    "    issues_employee.issue_id, "   +
                    "    issues_employee.job_id, "   +
                    "    employee.name, "   +
                    "    issues_employee.detail, "   +
                    "    issues_employee.picture, "   +
                    "    explanation, "   +
                    "    issues_employee.state, "   +
                    "    issues_employee.date "   +
                    "FROM   issues_employee "   +
                    "   INNER JOIN employee ON issues_employee.job_id = employee.job_id "   +
                    "WHERE  1=1 " +
                    "  <if test='userName != null and userName != \"\" '>" +
                    "     AND username = #{userName} AND state = #{state}" +
                    "  </if>"   +
                    "ORDER BY issues_employee.issue_id DESC " +
                    "</script>"
    })
    public List<Map<String, Object>> selectByCond1(@Param("userName") String userName,String state);

    @Select({
            "<script> " +
                    "SELECT "   +
                    "    MAX(issue_id) AS maxissueid "   +
                    "FROM  issues_employee"   +
                    "</script>"
    })
    public Map<String, String> selectMaxIssueId();



    @Select({
            "<script> " +
                    "SELECT "   +
                    "    job_id "   +
                    "FROM employee "   +
                    "WHERE  1=1 " +
                    "  <if test='userName != null and userName != \"\" '>" +
                    "     AND username = #{userName}" +
                    "  </if>"   +
                    "</script>"
    })
    public Map<String, String> selectJobId(@Param("userName") String userName);


    @Update(
            "<script> " +
                    "UPDATE issues_employee SET state=#{state} "  +
                    "WHERE "  +
                    "issue_id = #{issue_id} " +
            "</script>"
    )
    public void updateState(@Param("issue_id") String issue_id,@Param("state") String state);


    @Update(
            "<script> " +
                    "UPDATE issues_employee SET state=#{state} "  +
                    "  <if test='reason != null and reason != \"\" '>" +
                    ", maintenance_notqualified_reason = #{reason} "  +
                    "  </if>"   +
                    "WHERE "  +
                    "issue_id = #{issue_id} " +
                    "</script>"
    )
    public void maintainUnQualified2(@Param("issue_id") String issue_id, @Param("state")String state, @Param("reason")String reason);

}
